package com.lx.onbording.vshe.testassignment.core.component;

import org.apache.camel.Exchange;
import org.apache.camel.processor.aggregate.AggregationStrategy;
import org.springframework.stereotype.Component;

import com.lx.onbording.vshe.testassignment.core.domain.Order;
import com.lx.onbording.vshe.testassignment.core.domain.OrderItem;

@Component
public class OrderAgregationStrategy implements AggregationStrategy {

	@Override
	public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
		if (newExchange == null) {
			return oldExchange;
		}
		
		Order order = oldExchange.getIn().getBody(Order.class);
		Long shipingDepartmentId = newExchange.getIn().getBody(Long.class);
		
		for (OrderItem item : order.getOrderItems()) {
			item.setShippingDepartmentId(shipingDepartmentId);
		}
		
		oldExchange.getIn().setBody(order, Order.class);
		
		return oldExchange;
	}

}
